//响应式
$(function () {
        function setRem() {
            var clientWidth=$(window).width();
            var nowRem=(clientWidth/375*10);
            $("html").css("font-size",parseInt(nowRem,10)+"px");
        };
        setRem();

        $(function(){
            var timer;
            $(window).bind("resize",function () {
                clearTimeout(timer);
                timer=setTimeout(function () {
                    setRem();
                },100)
            })
        });
    });




//点击客户基本信息显示客户信息列表
function clientBaseInfo(){
	//获取显示信息列表内容
	//var clientInfo = getClientInfo();
	var clientList = $("#bag").html("");
	var clientPanel = "";
	var clientTable = "";
	//调用根据客户多条件查询出来的列表方法
	//var client = getClientInfo(null,null);
	
	clientPanel = "<div class='panel panel-info'>"+
	 "<!-- Default panel contents -->"+
	  "<div class='panel-heading'>" +
	  	"<div class='row'>"+
	  		"<div class='col-md-6 col-sm-6 col-xs-6'>"+
	  			"<span style='font-size:0.7rem'>客户基本信息</span>"+
	  		"</div>"+
	  		"<div class='col-md-3 col-sm-3 col-xs-3'>"+
	  		
	  		"</div>"+
	  	"</div>"+
	  	
	  "</div>"+
	  "<div>"+
	  "</div>"+
	  "<div class='row' style='margin-right:0px;margin-top:0.2rem'>"+
	  		"<div class='col-md-2 col-sm-2 col-xs-2'>"+
	  		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button class='btn btn-default' onclick='addClient()'>新&nbsp;&nbsp;增</button>"+
	  		"</div>"+
			"<div class='col-md-offset-8 col-sm-offset-8 col-xs-offset-8'>"+
			"<div class='input-group'>"+
	        "<input type='text' class='form-control' id='clientNames' placeholder='请输入信息'>"+
	        "<span class='input-group-btn'>"+
	          "<button class='btn btn-default' type='button' onclick='sel();'>查询</button>"+
	        "</span>"+
	      "</div>"+
			"</div>"+
	"</div>"+
	  
	  "<!-- Table -->"+
	  "<table class='table' style='text-align:center' id='clientLists'>"+
	  	"<thead>"+
	  		"<tr>"+
	  		"<th style='text-align:center'>客户名称</th>"+
	  		"<th style='text-align:center'>客户联系人</th>"+
	  		"<th style='text-align:center'>客户联系方式</th>"+
	  		"<th style='text-align:center'>客户地址</th>"+
	  		"<th style='text-align:center'>客户Email</th>"+
	  		"<th style='text-align:center'>传真</th>"+
	  		"<th style='text-align:center'>操&nbsp;&nbsp;作</th>"+
	  		"</tr>"+
	  	"</thead>"+
	    "<tbody>"+
	    "</tbody>"+
	  "</table>"+
	  "<div class='col-12-md' id='foot'>"+
	  "</div>"+
	"</div>";
	
		clientList.append(clientPanel);
		//调用显示客户列表信息的方法
		var data="name=&currentPages=";
		showClientInfo(data);
}

//按条件查询客户基本信息
function sel(){
	//获取查询条件
	var data="name="+$("#clientNames").val();
	showClientInfo(data);
}

//提交路径
function postMethod($url,$type,$data){
	var result="";
	$.ajax({
		"url":$url,
		"type":$type,
		"data":$data,
		"async":false,
		"success":function(response){
			
			result = response;
		},
		"dataType":"JSON"
	})
	return result;
}


//显示客户基本信息
function showClientInfo($data){
	
	var client = postMethod("/trade-wzj-web/ClientContrillerYang/clientSel","POST",$data);
	
	//获取结果集合
	var list = client.list;
	
	//获取查询条件
	var selCondition = $("#clientNames").val();
	
	var clientTable="";
	if(list.length>=0){
		for(var i=0;i<list.length;i++){
			clientTable = clientTable+"<tr>"+
    		"<td>"+list[i].clientName+"</td>"+
    		"<td>"+list[i].clientContacts+"</td>"+
    		"<td>"+list[i].clientPhone+"</td>"+
    		"<td>"+list[i].clientAddress+"</td>"+
    		"<td>"+list[i].clientEmail+"</td>"+
    		"<td>"+list[i].clientFax+"</td>"+
    		"<td><a href='#' onclick='updateClientOpen("+list[i].clientInfoID+","+client.pageNum+",\""+selCondition+"\");'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp;&nbsp;<a href='#' onclick='delClientTable("+list[i].clientInfoID+","+client.pageNum+",\""+selCondition+"\")'><span class='glyphicon glyphicon-remove'></span></a></td>"+
    		"</tr>";
		}
	}
	var tbody = $("#clientLists tbody").html("");
	tbody.append(clientTable);
	var page = getPageClient(client);
	var pageTable = $("#foot").html("");
	
	pageTable.append(page);
}

//获取页码信息
function getPageClient($clientList){
	var page="";
	
	var con = $("#clientNames").val();
	page = page+"<div class='row' style='margin:0px'>" +
	"<div class='col-md-2 col-sm-2 col-xs-2' style='padding-top:0.7rem'>"+
		"<span style='font-weight:bold'>总记录数 "+$clientList.total+"   |  总页数 "+$clientList.pages+"</span>"+
	"</div>"+
	"<div class='col-md-offset-8 col-sm-offset-8 col-xs-offset-8'>"
	+"<nav aria-label='Page navigation'>"+
"<ul class='pagination pagination-sm'>";
	if($clientList.prePage>0){
		page = page+"<li><a href='#' onclick='getPagesClient("+$clientList.firstPage+",\""+con+"\")'>首页</a></li>"+
		"<li><a href='#' onclick='getPagesClient("+$clientList.prePage+",\""+con+"\")'>上一页</a></li>";
				
	}
	if($clientList.nextPage<=$clientList.lastPage && $clientList.nextPage>0){
		page = page+"<li><a href='#' onclick='getPagesClient("+$clientList.nextPage+",\""+con+"\")'>下一页</a></li>"+
		"<li><a href='#' onclick='getPagesClient("+$clientList.lastPage+",\""+con+"\")'>末页</a></li>";
	}
	page = page+"</li>"+
"</ul>"+
"</nav>"+
"</div>"+
"</div>";
	return page;
	
}

//分页功能
function getPagesClient($page,$object){
	var data="name="+$object+"&currentPages="+$page;
	showClientInfo(data);
}


//删除客户基本信息
function delClientTable($delId,$pageNum,$object){
	var ensure = confirm("确定删除此信息吗?");
	
	if(ensure==false){
		return;
	}
	//获取删除条件
	var data="clientInfoId="+$delId;
	var url="/trade-wzj-web/testYu/test";
	var result = postMethod(url,"POST",data);
	console.info(result);
	if(result==""){
		alert("删除成功!");
	}else{
		alert(result);
	}
	
	var selData = "name="+$object+"&currentPages="+$pageNum;
	showClientInfo(selData);
}



//新增客户基本信息
function addClient(){
	var tables = $("#addClientTable");
	tables.html("");
	var tableAdd = "<tr>" +
			"<td>客户名称*：</td>" +
			"<td><input type='text' class='form-control' datatype='*' nullmsg='此信息不能为空' name='clientName' ></td>"+
			"<td></td>"+
			"</tr>"+
			"<tr>" +
			"<td>客户联系人*：</td>" +
			"<td><input type='text' class='form-control' datatype='*' nullmsg='不能为空' name='clientContacts'></td>"+
			"<td></td>"+
			"</tr>"+
			"<tr>" +
			"<td>客户联系方式*：</td>" +
			"<td><input type='text' name='clientPhone' datatype='*' nullmsg='不能为空'  class='form-control'></td>"+
			"<td></td>"+
			"</tr>"+
			"<tr>" +
			"<td>客户地址*：</td>" +
			"<td><input type='text' name='clientAddress' datatype='*' nullmsg='客户地址不能为空' class='form-control'></td>"+
			"<td></td>"+
			"</tr>"+
			"<tr>" +
			"<td>客户Email*：</td>" +
			"<td><input type='text' name='clientEmail' datatype='*' nullmsg='不能为空' class='form-control'></td>"+
			"<td></td>"+
			"</tr>"+
			"<tr>" +
			"<td>传真*：</td>" +
			"<td><input type='text' name='clientFax' datatype='*' nullmsg='不能为空' class='form-control'></td>"+
			"<td></td>"+
			"</tr>";
	tables.append(tableAdd);
	$("#addClient").modal("toggle");
}

//保存客户基本信息添加
function saveClient(){
	var addClientForm = $("#addClientForm").Validform({
		tiptype:1
	});
	
	var bool = addClientForm.check();
	if(bool==false){
		//var vss=$("#contp").html("");
		//window.showModalDialog();
		//$("#dialog").dialog();
		$("#addClientS").popover("toggle");
		return;
	}
	var data = $("#addClientForm").serialize();
	//获取提交路径
	var url="/trade-wzj-web/tests/kfAdd";
	var flag = postMethod(url,"POST",data);
	
	if(flag==""){
		alert("添加成功!");
		
	}else{
		alert("添加失败");
	}
	showClientInfo(null);
}

//打开要修改的客户基本信息页面
function updateClientOpen($updateId,$num,$obj){
	//定义要修改的对象
	var data="id="+$updateId;
	//获取提交路径
	var url="/trade-wzj-web/ClientContrillerYang/getClientInfoById";
	//调用提交方法
	var result = postMethod(url,"POST",data);
	if(result==""){
		alert("查询失败!");
		return;
	}
	var tables = $("#updateClientTable").html("");
	
	var tableAdd = "<tr>" +
			"<td>客户名称*：</td>" +
			"<td><input type='text' class='form-control' readonly='readonly' value='"+result.clientName+"'></td>"+
			"<td><input type='hidden' name='clientInfoID' value='"+result.clientInfoID+"' /></td>"+
			"</tr>"+
			"<tr>" +
			"<td>客户联系人*：</td>" +
			"<td><input type='text' class='form-control' datatype='*' nullmsg='不能为空' name='clientContacts' value='"+result.clientContacts+"' required></td>"+
			"<td></td>"+
			"</tr>"+
			"<tr>" +
			"<td>客户联系方式*：</td>" +
			"<td><input type='text' name='clientPhone' datatype='*' nullmsg='不能为空!' class='form-control' value='"+result.clientPhone+"' required></td>"+
			"<td><input type='hidden' id='updateNum' value="+$num+" /></td>"+
			"</tr>"+
			"<tr>" +
			"<td>客户地址*：</td>" +
			"<td><input type='text' value='"+result.clientAddress+"' name='clientAddress' datatype='*' class='form-control' required></td>"+
			"<td><input type='hidden' id='updatesName' value='"+$obj+"' /></td>"+
			"</tr>"+
			"<tr>" +
			"<td>客户Email*：</td>" +
			"<td><input type='text' value="+result.clientEmail+" name='clientEmail' datatype='*' class='form-control' required></td>"+
			"<td></td>"+
			"</tr>"+
			"<tr>" +
			"<td>传真*：</td>" +
			"<td><input type='text' value='"+result.clientFax+"' name='clientFax' datatype='*' class='form-control' required></td>"+
			"<td></td>"+
			"</tr>";
	tables.append(tableAdd);
	console.info(tableAdd);
	$("#updateClient").modal("toggle");
}

//修改客户基本信息按钮方法
function updateClient(){
	
	var updateClientForm = $("#updateClientForm").Validform({
		tiptype:1
	});
	var bool = updateClientForm.check();
	if(!bool){
		$("#updateClientS").popover("toggle");
		return;
	}
	var formData = $("#updateClientForm").serialize();
	//获取提交路径
	var url="/trade-wzj-web/ClientContrillerYang/updateClientInfoById";
	var result = postMethod(url,"POST",formData);
	console.info(result);
	if(result){
		alert("修改成功!");
	}else{
		alert("修改失败!");
	}
	//获取参数查询
	var $data="name="+$("#updatesName").val()+"&currentPages="+$("#updateNum").val();
	
	showClientInfo($data);
	
}
